package ocg.common.re.reports;

import ocg.common.re.Assertion;
import ocg.common.re.file.ReDate;
import org.testng.ITestResult;
import org.testng.annotations.*;

import java.lang.reflect.Method;
import java.text.ParseException;

public class ExtentTestNGReportBuilder {
    public static TestReport parentTest;
    public static TestReport test;
    public static String reportName;

    @BeforeSuite
    public void beforeSuite( ) {
        reportName= ReDate.today ();
        ExtentManager.build("test-output/"+reportName+".html");
    }

    @BeforeClass
    public synchronized void beforeClass() {
        parentTest = new TestReport( getClass().getSimpleName() ,"@Class");
        parentTest.log_info ("Test Start", ReDate.data () );
    }

    @BeforeMethod
    public synchronized void beforeMethod( Method method) {
        test = new TestReport( parentTest.get(),method.getName() , "@Method");
        Assertion.set(test);
    }

    @AfterMethod
    public synchronized void afterMethod(ITestResult result) {
        if (result.getStatus() == ITestResult.FAILURE)
            test.fail(result.getThrowable());
        else if (result.getStatus() == ITestResult.SKIP)
            test.skip(result.getThrowable());
        else
            test.pass("Test passed");
    }

    @AfterClass
    public synchronized void afterClass() throws ParseException {
        parentTest.log_info ("Test end", ReDate.data () );
        ExtentManager.flush();
    }
}
